package com.sea.base.utils

import android.graphics.Rect
import android.view.View
import androidx.annotation.Px
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.sea.base.ext.global.throwIfDebug

/**
 * 加间隔的
 * 注意：暂时没有处理header、footer，后续需要时添加
 * 由于这里是压缩view大小，所以对于类似FlexboxLayoutManager这种view本身是Wrap的可能会出现压缩变形的情况，所以不支持
 * 如果效果不太好，你可以使用rv的margin+item的margin来实现
 * @param horizontalSpace 两个之间的间隔
 * @param isLastSpace 最后是否留白（用于分页加载：上拉加载时不会刷新最后一个，如果不提前留好会比较难看）
 */
class SpacesItemDecoration(
    @Px private val horizontalSpace: Int,
    @Px private val verticalSpace: Int = horizontalSpace,
    private val isLastSpace: Boolean = true,
) : RecyclerView.ItemDecoration() {
    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        val manager = parent.layoutManager
        if (manager == null || (horizontalSpace <= 0 && verticalSpace <= 0)) {
            return
        }

        val spanCount: Int
        val orientation: Int
        when (manager) {
            is GridLayoutManager -> {
                orientation = manager.orientation
                spanCount = manager.spanCount
            }

            is LinearLayoutManager -> {
                orientation = manager.orientation
                spanCount = 1
            }

            is StaggeredGridLayoutManager -> {
                //由于不知道view具体摆在那里，所以上下左右各一半
                val horizontal2 = horizontalSpace / 2
                val vertical2 = verticalSpace / 2
                outRect.set(horizontal2, vertical2, horizontal2, vertical2)
                return
            }

            else -> {
                "由于这里是压缩view大小，所以对于类似FlexboxLayoutManager这种view本身是Wrap的可能会出现压缩变形的情况，所以不支持".throwIfDebug()
                return
            }
        }

        val position = parent.getChildAdapterPosition(view)

        val maxLine = ((parent.adapter?.itemCount ?: 0) + spanCount - 1) / spanCount
        val line = (position + spanCount) / spanCount//从1开始
        val column = position % spanCount + 1//从1开始

        // TODO: 待完善多行列大小问题
        val emptyLeft: Boolean
        val emptyRight: Boolean
        val emptyTop: Boolean
        val emptyBottom: Boolean
        if (orientation == RecyclerView.HORIZONTAL) {
            emptyLeft = line == 1
            emptyTop = column == spanCount
            emptyRight = if (isLastSpace) false else line == maxLine
            emptyBottom = column == 1
        } else {
            emptyLeft = column == 1
            emptyTop = line == 1
            emptyRight = column == spanCount
            emptyBottom = if (isLastSpace) false else line == maxLine
        }

        val horizontal2 = horizontalSpace / 2
        val vertical2 = verticalSpace / 2
        outRect.set(
            if (emptyLeft) 0 else horizontal2,
            if (emptyTop) 0 else vertical2,
            if (emptyRight) 0 else horizontal2,
            if (emptyBottom) 0 else vertical2
        )
    }

    override fun equals(other: Any?): Boolean {
        if (other is SpacesItemDecoration) {
            return other.horizontalSpace == horizontalSpace && other.verticalSpace == verticalSpace && other.isLastSpace == isLastSpace
        }
        return super.equals(other)
    }

    override fun hashCode(): Int {
        var result = horizontalSpace
        result = 31 * result + verticalSpace
        result = 31 * result + isLastSpace.hashCode()
        return result
    }
}